#include "game.h"
static int wu_point[17][17]={0};
static int space=0;
extern char chess_path[256];
extern int black_score,white_score,next_action;
int Photo_Change(int x,int y,int direction)   //返回歌曲暂停0，或者上一曲1，或者下一曲2
{
	if(x>=510 && x<=770 && y>=30 && y<=450)
	{
		printf("\ndirection=%d  ",direction);
		printf("past:x=%d y=%d",x,y);
		if(direction==1 || direction == 4)
		  return 1;
		else if(direction==2 || direction == 3)
		  return -1;
	}
	
	return 0;
}

void Go_point(int *XP,int *YP,int *mark_who)   //落棋函数
{
    
		int x2=(int)*XP;
		int y2=(int)*YP;

		//int x2=*XP;
		//int y2=*YP;
		
		int x2_p=(x2-60)/30+1;//坐标对于棋盘的X
		if((x2-60)%30>15)
		{
			x2_p=x2_p+1;
		}
		int y2_p=(y2-30)/30+1;//坐标对于棋盘的Y
		if((y2-30)%30>15)
		{
			y2_p=y2_p+1;
		}
		
		if(wu_point[x2_p][y2_p]==0 && *mark_who==1 && x2_p<17 && y2_p<17)//若（[x2_p]，[y2_p]）没有棋子，则下棋
		{
			wu_point[x2_p][y2_p]=1;
			yuan((x2_p-1)*30+60,(y2_p-1)*30+30,10,0x000000);//黑棋下棋结束
			printf("black_X: %d  balck_Y: %d\n",x2_p,y2_p);
			*mark_who=2;
            game1[space].x=*XP;
            game1[space].y=*YP;
            space++;
		}

		else if(wu_point[x2_p][y2_p]==0 && *mark_who==2 && x2_p<17 && y2_p<17)//若（[x2_p]，[y2_p]）没有棋子，则下棋
		{
			wu_point[x2_p][y2_p]=2;
			yuan((x2_p-1)*30+60,(y2_p-1)*30+30,10,0xFFFFFF);//白棋下棋结束
			printf("white_X: %d  white_Y: %d\n",x2_p,y2_p);
			*mark_who=1;
            game1[space].x=*XP;
            game1[space].y=*YP;
            space++;
		}
		*XP=x2_p;//把棋子在数组中对应的位置给XP，YP传出去
		*YP=y2_p;
		

}


int  panduan(int x,int y)     //判断是否胜利
{
    int temp;//保存下棋方的颜色
    int count=1;//统计个数,针对的同一线
    int i=1;//走一格
    int j=1;//和i同时用来代表走斜的
    int whoWin=0;//1代表黑方，2代表白方
    temp=wu_point[x][y];
    //水平的左边
    while(temp==wu_point[x-i][y]&&x>=5&&x<17&&y>=1&&y<17&&count<5)
    {
        i++;
        count++;
        if(count==5)//首先count是5才能来判断谁赢了，不然连资格都没有
        {
            if(temp==1)
                whoWin=1;

            else
                whoWin=2;
        }
    }
    //水平的右边
    i=1;
    while(temp==wu_point[x+i][y]&&x>0&&x<13&&y>0&&y<17&&count<5)
    {
        i++;
        count++;
        if(count==5)//首先count是5才能来判断谁赢了，不然连资格都没有
        {
            if(temp==1)
                whoWin=1;

            else
                whoWin=2;
        }
    }

    //解决垂直的方向
    //垂直上方
    i=1;
    count=1;//清理掉之前的数据
    while(temp==wu_point[x][y-i]&&x>0&&x<17&&y>4&&y<17&&count<5)
    {
        i++;
        count++;
        if(count==5)//首先count是5才能来判断谁赢了，不然连资格都没有
        {
            if(temp==1)
                whoWin=1;

            else
                whoWin=2;
        }
    }
    //垂直下方
    i=1;
    while(temp==wu_point[x][y+i]&&x>0&&x<17&&y>0&&y<13&&count<5)
    {
        i++;
        count++;
        if(count==5)//首先count是5才能来判断谁赢了，不然连资格都没有
        {
            if(temp==1)
                whoWin=1;

            else
                whoWin=2;
        }
    }
    //解决左上的斜线  
        i=1;
        j=1;
        count=1;
    while(temp==wu_point[x-i][y-j]&&x>4&&x<17&&y>4&&y<17&&count<5)
    {
        i++;
        j++;
        count++;
        if(count==5)//首先count是5才能来判断谁赢了，不然连资格都没有
        {
            if(temp==1)
                whoWin=1;

            else
                whoWin=2;
        }
    }
    //解决右下的斜线  
        i=1;
        j=1;
    while(temp==wu_point[x+i][y+j]&&x>0&&x<13&&y>0&&y<13&&count<5)
    {
        i++;
        j++;
        count++;
        if(count==5)//首先count是5才能来判断谁赢了，不然连资格都没有
        {
            if(temp==1)
                whoWin=1;

            else
                whoWin=2;
        }
    }
    //解决右上的斜线  
        i=1;
        j=1;
        count=1;
    while(temp==wu_point[x+i][y-j]&&x>0&&x<13&&y>4&&y<17&&count<5)
    {
        i++;
        j++;
        count++;
        if(count==5)//首先count是5才能来判断谁赢了，不然连资格都没有
        {
            if(temp==1)
                whoWin=1;

            else
                whoWin=2;
        }
    }
    //解决左下的斜线  
        i=1;
        j=1;
    while(temp==wu_point[x-i][y+j]&&x>4&&x<17&&y>0&&y<13&&count<5)
    {
        i++;
        j++;
        count++;
        if(count==5)//首先count是5才能来判断谁赢了，不然连资格都没有
        {
            if(temp==1)
                whoWin=1;

            else
                whoWin=2;
        }
    }
    return whoWin;
}

void Game_init(int *win_who,char path[512])    //游戏画面初始化
{
    LCD_Draw_All();
    if(chess_path[0]!='\0')
        JPEG_Display(chess_path, 0, 0);
	LCD_Draw_Chessboard();//画棋盘
	JPEG_Display(path, 510, 30);
	*win_who=0;
	for(int i=0;i<17;i++)
	{
		for(int j=0;j<17;j++)
		{
			
			if(i==0 ||i==16 ||j==0 ||j==16)
			{
				wu_point[i][j]=3;
			}
			else
			{
				wu_point[i][j]=0;
			}
		}
	}
}

void Game_resee(char path[512])                               //复盘界面
{
    int qi_sum=space;
    LCD_Draw_All();
    if(chess_path[0]!='\0')
        JPEG_Display(chess_path, 0, 0);
	LCD_Draw_Chessboard();//画棋盘
    int who_play=1,direction,flag=0;
    JPEG_Display(path, 510, 30);
    for(int i=0;i<17;i++)
	{
		for(int j=0;j<17;j++)
		{
			if(i==0 ||i==16 ||j==0 ||j==16)  
			{
				wu_point[i][j]=3;
			}
			else
			{
				wu_point[i][j]=0;
			}
		}
	}
    for(int i=0;i<qi_sum;i++)
    {
        int a=game1[i].x,b=game1[i].y;
        printf("\ngame.x=%d  game.y=%d",a,b);
        Go_point(&a,&b,&who_play);
        usleep(300000);
        // int X,Y;
        // X=0,Y=0;
        // Get_Point(&X,&Y);
        // do
        // {
        //     Get_Point(&X,&Y);
        // }while (X>=510 && X<=770 &&Y>=30 && Y<=450);
            
    }
    
    for(int i=0;i<qi_sum;i++)
    {
        game1[i].x=0;
        game1[i].y=0;
    }
    space=0;
}

void init_chessboard(char* init_chessboard)                              //初始化棋牌
{
    int XP,YP;
    while(1)
    {
        LCD_Draw_All_white();
        JPEG_Display("/home/xc/wuzi/game/4.jpg",0,0);
        JPEG_Display("/home/xc/wuzi/game/5.jpg",0,200);
        JPEG_Display("/home/xc/wuzi/game/6.jpg",270,200);
        JPEG_Display("/home/xc/wuzi/game/7.jpg",540,200);
        Get_Point(&XP,&YP);
        if(XP >=0 &&XP<=260 &&YP>=200&& YP<=360)
        {
            strcpy(init_chessboard,"/home/xc/wuzi/game/21.jpg");
        }
        else if(XP >=270 &&XP<=530 &&YP>=200&& YP<=360)
        {
            strcpy(init_chessboard,"/home/xc/wuzi/game/22.jpg");
        }
        else if(XP >=540 &&XP<=800 &&YP>=200&& YP<=360)
        {
            strcpy(init_chessboard,"/home/xc/wuzi/game/23.jpg");
        }
        else if(XP >0 &&XP<=50 &&YP>0&& YP<=50)
        {
            break;
        }
        else { 
            strcpy(init_chessboard,"\0");
        }
    }
}

void Game_Last(void)                             //
{
    LCD_Draw_All_white();
    JPEG_Display("/home/xc/wuzi/game/24.jpg",300, 100);
    JPEG_Display("/home/xc/wuzi/game/25.jpg",300, 170);
    JPEG_Display("/home/xc/wuzi/game/26.jpg",300, 240);
    JPEG_Display("/home/xc/wuzi/game/27.jpg",300, 430);
    lowercase(0,0,"score",2,2,0x000000);
    //中间隔了10
    Math_L(106,0,white_score,2,2,0x0);
    Min_Draw(122,0,cr,2,2,0x0);
    Math_R(138,0,black_score,2,2,0x0);
}
int music=0;           //music=1音乐打开
void Game_Begin(void)
{
    int XP,YP;
    
    while(1)
    {
        JPEG_Display("/home/xc/wuzi/game/1.jpg",0,0);
        JPEG_Display("/home/xc/wuzi/game/2.jpg", 50, 400);
        JPEG_Display("/home/xc/wuzi/game/music_prev.jpg",0,0);
        if(music==0)
        {
            JPEG_Display("/home/xc/wuzi/game/music_on.jpg",100,0);
        }
        else
        {
            JPEG_Display("/home/xc/wuzi/game/music_stop.jpg",100,0);
        }
        JPEG_Display("/home/xc/wuzi/game/music_next.jpg",200,0);
        Get_Point(&XP,&YP);
        if(XP <=800 &&XP>=750 &&YP>0&& YP<=50)
            init_chessboard(chess_path);
        if(XP >=50 &&XP<=150 &&YP>400&& YP<=450)
            break;
        if(XP >=0 &&XP<=100 &&YP>0&& YP<=100)
            next_action=1;
        if(XP >=100 &&XP<=200 &&YP>0&& YP<=100)
        {
            music=(music+1)%2;
            next_action=2;
        }                              //暂停
        if(XP >=200 &&XP<=300 &&YP>0&& YP<=100)
            next_action=3;
        printf("next_action=%d,music=%d\n",next_action,music);
    }
}